﻿using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using HslCommunication.ModBus;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.Inovance
{
	/// <summary>
	/// 汇川的网络通信协议，适用于H3U, XP 等系列，底层走的是MODBUS-TCP协议，地址说明参见标记<br />
	/// Huichuan's network communication protocol is suitable for H3U, XP and other series. The bottom layer is MODBUS-TCP protocol. For the address description, please refer to the mark
	/// </summary>
	/// <remarks>
	/// 本组件适用的系列为H3U 系列和 XP 系列 PLC（H2UXP、 H2SXP、 H1UXP、 H1SXP 等系列） 其中XP 系列控制器不支持以太网通讯<br />
	/// H3U 系列控制器支持 M/SM/S/T/C/X/Y 等 bit 型变量（也称线圈） 的访问、 D/SD/R/T/C 等 word 型变量的访问；<br /><br />
	/// <c>我们先来看看H3U系列</c><br />
	/// 线圈、 位元件、位变量地址定义
	/// <list type="table">
	///   <listheader>
	///     <term>地址名称</term>
	///     <term>地址代号</term>
	///     <term>地址范围</term>
	///     <term>地址进制</term>
	///     <term>备注</term>
	///   </listheader>
	///   <item>
	///     <term>中间寄电器</term>
	///     <term>M</term>
	///     <term>M0-M7679，M8000-M8511</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term></term>
	///     <term>SM</term>
	///     <term>SM0-SM1023</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term></term>
	///     <term>S</term>
	///     <term>S0-S4095</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>定时器</term>
	///     <term>T</term>
	///     <term>T0-T511</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>计数器</term>
	///     <term>C</term>
	///     <term>C0-C255</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>输入</term>
	///     <term>X</term>
	///     <term>X0-X377 或者X0.0-X37.7</term>
	///     <term>8</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>输出</term>
	///     <term>Y</term>
	///     <term>Y0-Y377 或者Y0.0-Y37.7</term>
	///     <term>8</term>
	///     <term></term>
	///   </item>
	/// </list>
	/// 寄存器、 字元件、字变量地址定义：
	/// <list type="table">
	///   <listheader>
	///     <term>地址名称</term>
	///     <term>地址代号</term>
	///     <term>地址范围</term>
	///     <term>地址进制</term>
	///     <term>备注</term>
	///   </listheader>
	///   <item>
	///     <term>数据寄存器</term>
	///     <term>D</term>
	///     <term>D0-D8511</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>数据寄存器</term>
	///     <term>SD</term>
	///     <term>SD0-SD1023</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term></term>
	///     <term>R</term>
	///     <term>R0-R32767</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>定时器</term>
	///     <term>T</term>
	///     <term>T0-T511</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>计数器</term>
	///     <term>C</term>
	///     <term>C0-C199,C200-C255</term>
	///     <term>10</term>
	///     <term>其实C200-C255的计数器是32位的</term>
	///   </item>
	/// </list>
	/// <c>我们再来看看XP系列，就是少了一点访问的数据类型，然后，地址范围也不一致</c><br />
	/// 线圈、 位元件、位变量地址定义
	/// <list type="table">
	///   <listheader>
	///     <term>地址名称</term>
	///     <term>地址代号</term>
	///     <term>地址范围</term>
	///     <term>地址进制</term>
	///     <term>备注</term>
	///   </listheader>
	///   <item>
	///     <term>中间寄电器</term>
	///     <term>M</term>
	///     <term>M0-M3071，M8000-M8511</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term></term>
	///     <term>S</term>
	///     <term>S0-S999</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>定时器</term>
	///     <term>T</term>
	///     <term>T0-T255</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>计数器</term>
	///     <term>C</term>
	///     <term>C0-C255</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>输入</term>
	///     <term>X</term>
	///     <term>X0-X377 或者X0.0-X37.7</term>
	///     <term>8</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>输出</term>
	///     <term>Y</term>
	///     <term>Y0-Y377 或者Y0.0-Y37.7</term>
	///     <term>8</term>
	///     <term></term>
	///   </item>
	/// </list>
	/// 寄存器、 字元件、字变量地址定义：
	/// <list type="table">
	///   <listheader>
	///     <term>地址名称</term>
	///     <term>地址代号</term>
	///     <term>地址范围</term>
	///     <term>地址进制</term>
	///     <term>备注</term>
	///   </listheader>
	///   <item>
	///     <term>数据寄存器</term>
	///     <term>D</term>
	///     <term>D0-D8511</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>定时器</term>
	///     <term>T</term>
	///     <term>T0-T255</term>
	///     <term>10</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>计数器</term>
	///     <term>C</term>
	///     <term>C0-C199,C200-C255</term>
	///     <term>10</term>
	///     <term>其实C200-C255的计数器是32位的</term>
	///   </item>
	/// </list>
	/// </remarks>
	// Token: 0x02000096 RID: 150
	public class InovanceH3UTcp : ModbusTcpNet
	{
		/// <summary>
		/// 实例化一个汇川H3U, XP 系列的网络通讯协议<br />
		/// Instantiate a network communication protocol of HuiChuan H3U, XP series
		/// </summary>
		// Token: 0x06000B51 RID: 2897 RVA: 0x0004272E File Offset: 0x0004092E
		public InovanceH3UTcp()
		{
		}

		/// <summary>
		/// 指定服务器地址，端口号，客户端自己的站号来实例化一个汇川H3U, XP 系列的网络通讯协议<br />
		/// Specify the server address, port number, and client's own station number to instantiate a network communication protocol of Huichuan H3U, XP series
		/// </summary>
		/// <param name="ipAddress">服务器的Ip地址</param>
		/// <param name="port">服务器的端口号</param>
		/// <param name="station">客户端自身的站号</param>
		// Token: 0x06000B52 RID: 2898 RVA: 0x00042738 File Offset: 0x00040938
		public InovanceH3UTcp(string ipAddress, int port = 502, byte station = 1) : base(ipAddress, port, station)
		{
		}

		/// <summary>
		/// 按字读取汇川PLC的数据信息，对于H3U情况，可以输入D0,SD0,R0,T0,C0类型地址，对于XP系列而言，可以输入D0,T0,C0<br />
		/// Read Huichuan PLC's data information by word. For H3U, you can enter D0, SD0, R0, T0, C0 type addresses. For XP series, you can enter D0, T0, C0.
		/// </summary>
		/// <param name="address">PLC的真实的地址信息，对于H3U情况，可以输入D0,SD0,R0,T0,C0类型地址，对于XP系列而言，可以输入D0,T0,C0</param>
		/// <param name="length">读取的数据的长度，按照字为单位</param>
		/// <returns>包含是否成功的结果对象信息</returns>
		// Token: 0x06000B53 RID: 2899 RVA: 0x00042D94 File Offset: 0x00040F94
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<string> operateResult = InovanceHelper.PraseInovanceH3UAddress(address, 3);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				result = base.Read(operateResult.Content, length);
			}
			return result;
		}

		/// <summary>
		/// 按字写入汇川PLC的数据信息，对于H3U情况，可以输入D0,SD0,R0,T0,C0类型地址，对于XP系列而言，可以输入D0,T0,C0<br />
		/// Write the data information of Huichuan PLC by word. For H3U, you can enter D0, SD0, R0, T0, and C0 type addresses. For XP series, you can enter D0, T0, and C0.
		/// </summary>
		/// <param name="address">PLC的真实的地址信息，对于H3U情况，可以输入D0,SD0,R0,T0,C0类型地址，对于XP系列而言，可以输入D0,T0,C0</param>
		/// <param name="value">等待写入的原始数据，长度为2的倍数</param>
		/// <returns>是否写入成功的结果信息</returns>
		// Token: 0x06000B54 RID: 2900 RVA: 0x00042DD4 File Offset: 0x00040FD4
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			OperateResult<string> operateResult = InovanceHelper.PraseInovanceH3UAddress(address, 16);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, value);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Inovance.InovanceH3UTcp.Read(System.String,System.UInt16)" />
		// Token: 0x06000B55 RID: 2901 RVA: 0x00042E14 File Offset: 0x00041014
		[DebuggerStepThrough]
		public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
		{
			InovanceH3UTcp.<ReadAsync>d__4 <ReadAsync>d__ = new InovanceH3UTcp.<ReadAsync>d__4();
			<ReadAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadAsync>d__.<>4__this = this;
			<ReadAsync>d__.address = address;
			<ReadAsync>d__.length = length;
			<ReadAsync>d__.<>1__state = -1;
			<ReadAsync>d__.<>t__builder.Start<InovanceH3UTcp.<ReadAsync>d__4>(ref <ReadAsync>d__);
			return <ReadAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Inovance.InovanceH3UTcp.Write(System.String,System.Byte[])" />
		// Token: 0x06000B56 RID: 2902 RVA: 0x00042E68 File Offset: 0x00041068
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, byte[] value)
		{
			InovanceH3UTcp.<WriteAsync>d__5 <WriteAsync>d__ = new InovanceH3UTcp.<WriteAsync>d__5();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<InovanceH3UTcp.<WriteAsync>d__5>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 按位读取汇川PLC的数据信息，对于H3U情况，可以输入M0,SM0,S0,T0,C0,X0,Y0类型地址，对于XP系列而言，可以输入M0,S0,T0,C0,X0,Y0<br />
		/// Read the data information of Huichuan PLC bit by bit. For H3U, you can enter M0, SM0, S0, T0, C0, X0, Y0 type addresses. For XP series, you can enter M0, S0, T0, C0, X0. , Y0
		/// </summary>
		/// <param name="address">汇川PLC的真实的位地址信息，对于H3U情况，可以输入M0,SM0,S0,T0,C0,X0,Y0类型地址，对于XP系列而言，可以输入M0,S0,T0,C0,X0,Y0</param>
		/// <param name="length">等待读取的长度，按照位为单位</param>
		/// <returns>包含是否成功的结果对象</returns>
		// Token: 0x06000B57 RID: 2903 RVA: 0x00042EBC File Offset: 0x000410BC
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			OperateResult<string> operateResult = InovanceHelper.PraseInovanceH3UAddress(address, 1);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.ReadBool(operateResult.Content, length);
			}
			return result;
		}

		/// <summary>
		/// 按位写入汇川PLC的数据信息，对于H3U情况，可以输入M0,SM0,S0,T0,C0,X0,Y0类型地址，对于XP系列而言，可以输入M0,S0,T0,C0,X0,Y0<br />
		/// Write the data information of Huichuan PLC bit by bit. For H3U, you can enter M0, SM0, S0, T0, C0, X0, Y0 type addresses. For XP series, you can enter M0, S0, T0, C0, X0 , Y0
		/// </summary>
		/// <param name="address">汇川PLC的真实的位地址信息，对于H3U情况，可以输入M0,SM0,S0,T0,C0,X0,Y0类型地址，对于XP系列而言，可以输入M0,S0,T0,C0,X0,Y0</param>
		/// <param name="values">等待写入的原始数据</param>
		/// <returns>是否写入成功</returns>
		// Token: 0x06000B58 RID: 2904 RVA: 0x00042EFC File Offset: 0x000410FC
		[HslMqttApi("WriteBoolArray", "")]
		public override OperateResult Write(string address, bool[] values)
		{
			OperateResult<string> operateResult = InovanceHelper.PraseInovanceH3UAddress(address, 15);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, values);
			}
			return result;
		}

		/// <summary>
		/// 写入汇川PLC一个bool数据，对于H3U情况，可以输入M0,SM0,S0,T0,C0,X0,Y0类型地址，对于XP系列而言，可以输入M0,S0,T0,C0,X0,Y0<br />
		/// Write a bool data to Huichuan PLC. For H3U, you can enter M0, SM0, S0, T0, C0, X0, Y0 type address. For XP series, you can enter M0, S0, T0, C0, X0, Y0.
		/// </summary>
		/// <param name="address">汇川PLC的真实的位地址信息，对于H3U情况，可以输入M0,SM0,S0,T0,C0,X0,Y0类型地址，对于XP系列而言，可以输入M0,S0,T0,C0,X0,Y0</param>
		/// <param name="value">bool数据值</param>
		/// <returns>是否写入成功</returns>
		// Token: 0x06000B59 RID: 2905 RVA: 0x00042F3C File Offset: 0x0004113C
		[HslMqttApi("WriteBool", "")]
		public override OperateResult Write(string address, bool value)
		{
			OperateResult<string> operateResult = InovanceHelper.PraseInovanceH3UAddress(address, 5);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, value);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Inovance.InovanceH3UTcp.ReadBool(System.String,System.UInt16)" />
		// Token: 0x06000B5A RID: 2906 RVA: 0x00042F7C File Offset: 0x0004117C
		[DebuggerStepThrough]
		public override Task<OperateResult<bool[]>> ReadBoolAsync(string address, ushort length)
		{
			InovanceH3UTcp.<ReadBoolAsync>d__9 <ReadBoolAsync>d__ = new InovanceH3UTcp.<ReadBoolAsync>d__9();
			<ReadBoolAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
			<ReadBoolAsync>d__.<>4__this = this;
			<ReadBoolAsync>d__.address = address;
			<ReadBoolAsync>d__.length = length;
			<ReadBoolAsync>d__.<>1__state = -1;
			<ReadBoolAsync>d__.<>t__builder.Start<InovanceH3UTcp.<ReadBoolAsync>d__9>(ref <ReadBoolAsync>d__);
			return <ReadBoolAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Inovance.InovanceH3UTcp.Write(System.String,System.Boolean[])" />
		// Token: 0x06000B5B RID: 2907 RVA: 0x00042FD0 File Offset: 0x000411D0
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, bool[] values)
		{
			InovanceH3UTcp.<WriteAsync>d__10 <WriteAsync>d__ = new InovanceH3UTcp.<WriteAsync>d__10();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.values = values;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<InovanceH3UTcp.<WriteAsync>d__10>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Inovance.InovanceH3UTcp.Write(System.String,System.Boolean)" />
		// Token: 0x06000B5C RID: 2908 RVA: 0x00043024 File Offset: 0x00041224
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, bool value)
		{
			InovanceH3UTcp.<WriteAsync>d__11 <WriteAsync>d__ = new InovanceH3UTcp.<WriteAsync>d__11();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<InovanceH3UTcp.<WriteAsync>d__11>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 写入汇川PLC的一个字数据，对于H3U情况，可以输入D0,SD0,R0,T0,C0类型地址，对于XP系列而言，可以输入D0,T0,C0<br />
		/// Write one word data of Huichuan PLC. For H3U, you can enter D0, SD0, R0, T0, C0 type address. For XP series, you can enter D0, T0, C0
		/// </summary>
		/// <param name="address">汇川PLC的真实地址，对于H3U情况，可以输入D0,SD0,R0,T0,C0类型地址，对于XP系列而言，可以输入D0,T0,C0</param>
		/// <param name="value">short数据</param>
		/// <returns>是否写入成功</returns>
		// Token: 0x06000B5D RID: 2909 RVA: 0x00043078 File Offset: 0x00041278
		[HslMqttApi("WriteInt16", "")]
		public override OperateResult Write(string address, short value)
		{
			OperateResult<string> operateResult = InovanceHelper.PraseInovanceH3UAddress(address, 6);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, value);
			}
			return result;
		}

		/// <summary>
		/// 写入汇川PLC的一个字数据，对于H3U情况，可以输入D0,SD0,R0,T0,C0类型地址，对于XP系列而言，可以输入D0,T0,C0<br />
		/// Write one word data of Huichuan PLC. For H3U, you can enter D0, SD0, R0, T0, C0 type address. For XP series, you can enter D0, T0, C0
		/// </summary>
		/// <param name="address">汇川PLC的真实地址，对于H3U情况，可以输入D0,SD0,R0,T0,C0类型地址，对于XP系列而言，可以输入D0,T0,C0</param>
		/// <param name="value">ushort数据</param>
		/// <returns>是否写入成功</returns>
		// Token: 0x06000B5E RID: 2910 RVA: 0x000430B8 File Offset: 0x000412B8
		[HslMqttApi("WriteUInt16", "")]
		public override OperateResult Write(string address, ushort value)
		{
			OperateResult<string> operateResult = InovanceHelper.PraseInovanceH3UAddress(address, 6);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, value);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Inovance.InovanceH3UTcp.Write(System.String,System.Int16)" />
		// Token: 0x06000B5F RID: 2911 RVA: 0x000430F8 File Offset: 0x000412F8
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, short value)
		{
			InovanceH3UTcp.<WriteAsync>d__14 <WriteAsync>d__ = new InovanceH3UTcp.<WriteAsync>d__14();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<InovanceH3UTcp.<WriteAsync>d__14>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Inovance.InovanceH3UTcp.Write(System.String,System.UInt16)" />
		// Token: 0x06000B60 RID: 2912 RVA: 0x0004314C File Offset: 0x0004134C
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, ushort value)
		{
			InovanceH3UTcp.<WriteAsync>d__15 <WriteAsync>d__ = new InovanceH3UTcp.<WriteAsync>d__15();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<InovanceH3UTcp.<WriteAsync>d__15>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000B61 RID: 2913 RVA: 0x0004319E File Offset: 0x0004139E
		public override string ToString()
		{
			return string.Format("InovanceH3UTcp[{0}:{1}]", this.IpAddress, this.Port);
		}
	}
}
